/**
 *
 */
package com.priox.sigar.application.writer;

import com.priox.sigar.application.acreedor.RegistroAcreedorApp;
import com.priox.sigar.application.cuentacontable.RegistroCuentaContableApp;
import com.priox.sigar.application.sociedad.RegistroSociedadApp;
import com.priox.sigar.client.dto.*;
import com.priox.sigar.client.flt.CuentaContableFlt;
import com.priox.sigar.domain.model.constantes.Constantes;
import com.priox.sigar.enums.TipoCuenta;
import com.priox.sigar.server.infrastructure.persistence.util.DateUtil;
import com.priox.sigar.util.TimeObserver;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

@Component
public class BalanceComparacionItemWriter implements ItemWriter<BalanceComprobacionDTO> {

    private static final Logger LOGGER = LoggerFactory.getLogger("jobLog");

    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void init() {
        this.jdbcTemplate = new JdbcTemplate(dataSource);

    }


    /* (non-Javadoc)
     * @see org.springframework.batch.item.ItemWriter#write(java.util.List)
     */
    public void write(final List<? extends BalanceComprobacionDTO> items) throws Exception {

        String sql = "INSERT INTO [dbo].[SGTM_BALANCE_COMPROBACION]([CUENTA_CONTABLE_ID], [SOCIEDAD_ID], [CHECKPOINT_ID], [SALDO_INICIAL]) " +
                "    VALUES ( ?,?,?,?)";

        try {

            Long checkPointId = null;
            if (items.size() > 0) {
                checkPointId = items.get(0).getCheckPointId();
            }

            TimeObserver timeObserver = new TimeObserver("insert del bloque en ");
            timeObserver.empezar();
            int[] rows = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

                public void setValues(PreparedStatement ps, int i) throws SQLException {

                    BalanceComprobacionDTO customer = items.get(i);

                    ps.setString(1, customer.getCuentaContableId());
                    ps.setString(2, customer.getSociedadId());
                    ps.setLong(3, customer.getCheckPointId());
                    if (customer.getSaldoInicial() != null) {
                        ps.setDouble(4, customer.getSaldoInicial().doubleValue());
                    } else {
                        ps.setDouble(4, 0);
                    }
                }
                public int getBatchSize() {
                    return items.size();
                }
            });

            timeObserver.terminar();
            LOGGER.info(timeObserver.mostrarMsg() + " -> rows " + rows.length);
        } catch (Exception ex) {
            LOGGER.debug("registros en bd -> rows ", ex);
            throw ex;
        }

    }


}
